package com.example.demo;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.PointFEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    private Button btnStart;
    private String TAG="MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnStart = findViewById(R.id.btn_start);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // starAnimator();
                // startAnimatorWithXML();
                startAnimatorofObject();
            }
        });
    }

    private void startAnimatorofObject() {
        Point pointStart = new Point(100,100);
        Point pointEnd = new Point(400,600);
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvaluator(),pointStart,pointEnd);
        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                Point point = (Point) animation.getAnimatedValue();
                Log.e(TAG, "point x = "+point.getX()+" y = "+point.getY());
                btnStart.getLayoutParams().width = point.getX();
                btnStart.getLayoutParams().height = point.getY();
                btnStart.requestLayout();
            }
        });
        valueAnimator.start();
    }

    private void startAnimatorWithXML() {

        Animator animator = AnimatorInflater.loadAnimator(MainActivity.this,R.animator.set_animator);
        animator.setTarget(btnStart);
        ValueAnimator valueAnimator = (ValueAnimator) animator;
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                Log.e(TAG, "value =  " + value);
                btnStart.getLayoutParams().width = value;
                btnStart.getLayoutParams().height = value;
                btnStart.setText("开始"+value);
                btnStart.requestLayout();
                btnStart.setBackgroundColor(Color.argb(255,value/2,0,0));
            }
        });
        animator.start();
    }

    private void starAnimator() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(btnStart.getLayoutParams().width,500);
        valueAnimator.setDuration(5000);
        valueAnimator.setStartDelay(500);
        valueAnimator.setRepeatCount(-1);
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                Log.e(TAG, "value =  " + value);
                btnStart.getLayoutParams().width = value;
                btnStart.getLayoutParams().height = value;
                btnStart.setText("开始"+value);
                btnStart.requestLayout();
            }
        });
        valueAnimator.start();
    }
}
